import _ from 'lodash'
import Konva from 'konva'
import ebus from './ebus.js'
import * as lcd from './lcd.js'
import {printObj, copyObj} from './utils.js'

export function VLayout() {
  this.setMode = function(mode) {
    let stage = this.stage
    let userdata = stage.userdata

    if (mode === 'newWindow') {

      lcd.addNewWindowEvent(stage, userdata.layoutLayer)
      lcd.delSetWindowEvent(stage)
    } else if (mode === 'setWindow') {
      // console.log('drop',stage)
      lcd.delNewWindowEvent(stage)
      lcd.addSetWindowEvent(stage)
    }
  }
  
  this.checkWindowOverlay = function() {
    return lcd.checkWindowOverlay(this.stage)
  }

  this.getWindowList = function() {
    return lcd.getWindowList(this.stage)
  }

  this.getWindowListCnt = function() {
    return lcd.getWindowListCnt(this.stage)
  }

  this.updateByWindowList = function(windowList) {
   
    let stage = this.stage
    let userdata = stage.userdata
    
    lcd.delAllWindows(stage, userdata.layoutLayer)

    let sortedWindows = _.sortBy(windowList, ['layer'])
    _.forEach(sortedWindows, function(item) {
      lcd.openWindow(stage, userdata.layoutLayer, item)
    })

    this.setMode('setWindow')
  }

  function initUserDataWithCfg(userdata, cfg) {
    cfg.unitXCnt = cfg.multiScreenX * 12
    cfg.unitYCnt = cfg.multiScreenY * 12
    // cfg.unitWidth = cfg.width / cfg.base.width
    // cfg.unitHeight = cfg.height / cfg.base.height  
    cfg.unitWidth = cfg.width / cfg.unitXCnt
    cfg.unitHeight = cfg.height / cfg.unitYCnt  

    userdata.cfg = cfg
  }

  function isOverLap(rc1, rc2) {
    //矩形是否重叠
    if (rc1.x + rc1.width  > rc2.x &&
      rc2.x + rc2.width  > rc1.x &&
      rc1.y + rc1.height > rc2.y &&
      rc2.y + rc2.height > rc1.y
     )
        return true;
    else
        return false;
  }

  this.init = function(container, cfg) {
    if (this.stage) {
      ebus.off('reset-lcd')
      ebus.off('layout-new-window-end')      
      this.stage.destroy()
      this.stage = undefined
    }

    this.stage = new Konva.Stage({
      container: container,
      width: cfg.width,
      height: cfg.height
    })

    var userdata = {}
    initUserDataWithCfg(userdata, cfg)
    userdata.windows = []
    userdata.dftLayer = new Konva.Layer()
    userdata.layoutLayer = new Konva.Layer()
    this.stage.userdata = userdata
    this.stage.add(userdata.dftLayer, userdata.layoutLayer)

    ebus.on('layout-reset-lcd', function() {
      lcd.updateWindows(this.stage, userdata.layoutLayer)
      let totalwindow = this.stage.userdata.windows
      for(let i = 0; i < totalwindow.length; i++){
        for(let j = i+1; j < totalwindow.length; j++) {
          let overlap =  isOverLap(totalwindow[i].windowRect,totalwindow[j].windowRect)
          if(overlap) {
              ebus.emit('overlapping',true)
              console.log('重叠了1')
              return
          }
        }
        
      }
      ebus.emit('overlapping',false)

      
    }.bind(this))

    lcd.drawLayoutGrid(userdata.dftLayer, userdata.cfg)

    ebus.on('layout-new-window-end', function() {
      this.setMode('setWindow')
      console.log('drop',this.stage)
      let totalwindow = this.stage.userdata.windows
      if( totalwindow.length > 1) {
        let selectRect = totalwindow[totalwindow.length - 1].windowRect
        //比较是否重叠
        for(let i = 0; i < totalwindow.length - 1; i++){
          let overlap =  isOverLap(selectRect,totalwindow[i].windowRect)
          if(overlap) {
            ebus.emit('overlapping',true)
            console.log('重叠了2')
            return
          }
        }
      }
      
    }.bind(this))
    this.stage.on('drop', e => {
      // debugger
    })
    this.stage.on('dragend', e => {
      console.log('dragend',e)
      // debugger
    })
    this.stage.on('dragover', e => {
      console.log('dragover',e)
      // debugger
    })
  }

  this.uninit = function() {
    ebus.off('reset-lcd')
    ebus.off('layout-new-window-end')
    this.stage && this.stage.destroy()
    this.stage = undefined
  }
}